package com.liuyjy.exception.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author liuyjy
 * @Date 2020/4/17 13:15
 * @Description: 配置
 **/
@Configuration
//@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class ExceptionConfiguration {


//    @Bean
//    public Validator validator() {
//        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
//                .configure()
//                //failFast的意思只要出现校验失败的情况，就立即结束校验，不再进行后续的校验。
//                .failFast(false)
//                .buildValidatorFactory();
//        return validatorFactory.getValidator();
//    }
//
//    @Bean
//    public MethodValidationPostProcessor methodValidationPostProcessor() {
//        MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
//        methodValidationPostProcessor.setValidator(validator());
//        return methodValidationPostProcessor;
//    }


    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "liuyjy.common.except", name = "type", havingValue = "remote")
    public RemoteExceptionHandler remoteExceptionHandler() {
        return new RemoteExceptionHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "liuyjy.common.except", name = "type", havingValue = "global")
    public GlobalExceptionHandler globalExceptionHandler() {
        return new GlobalExceptionHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public FeignErrorDecoder feignErrorDecoder() {
        return new FeignErrorDecoder();
    }

}
